package com.team2.outpatient_sys.web.administrator;

import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.pojo.Department;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.service.AdminDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dept")
public class AdminDepartmentController {
    @Autowired
    private AdminDepartmentService adminDepartmentService;

    /**
     * 处理添加新科室请求的Controller方法
     * @param department post请求体中的department数据封装的Department对象
     * @return 响应内容，添加新科室结果封装的Result对象
     */
    @PostMapping
    public Result addNew(@RequestBody Department department) {
        boolean isAddSuccess = adminDepartmentService.addNew(department);
        return isAddSuccess ? new Result(200, "添加新科室成功")
                : new Result(500, "添加新科室时发生异常");
    }

    /**
     * 处理修改科室信息请求的Controller方法
     * 通过Department对象逐一修改某一id科室信息的所有字段
     * @param department put请求体中的department数据封装的Department对象
     * @return 响应内容，修改科室信息结果封装的Result对象
     */
    @PutMapping
    public Result updateWhole(@RequestBody Department department) {
        boolean isUpdateSuccess = adminDepartmentService.updateWhole(department);
        return isUpdateSuccess ? new Result(200, "完整科室信息修改成功")
                : new Result(500, "修改科室信息时发生异常");
    }

    /**
     * 处理根据id删除科室信息请求的Controller方法
     * @param departmentId 需要删除的科室信息的id
     * @return 响应内容，删除科室信息结果封装的Result对象
     */
    @DeleteMapping("/{departmentId}")
    public Result deleteById(@PathVariable Integer departmentId) {
        boolean isDeleteSuccess = adminDepartmentService.deleteById(departmentId);
        return isDeleteSuccess ? new Result(200, "根据departmentId删除数据成功")
                : new Result(500, "根据departmentId删除数据时发生异常");
    }

    /**
     * 处理查询所有科室信息请求的Controller方法
     * @return 响应内容，查询所有科室信息结果封装的Result对象
     *         包含查询到的所有科室信息的List集合
     */
    @GetMapping
    public Result getAll() {
        List<Department> departmentList = adminDepartmentService.getAll();
        Result result = new Result();
        if (departmentList == null) {
            result.setCode(500);
            result.setMsg("所有科室信息查询时发生异常");
        } else {
            result.setCode(200);
            result.setMsg("所有科室信息查询成功");
            result.setData(departmentList);
        }
        return result;
    }

    /**
     * 处理根据id查询对应科室信息请求的Controller方法
     * @param deptId 需要查询的科室信息的id
     * @return 响应内容，查询的对应科室信息结果封装的Result对象
     */
    @GetMapping("/{deptId}")
    public Result getById(@PathVariable Integer deptId) {
        Department department = adminDepartmentService.getById(deptId);
        Result result = new Result();
        if (department == null) {
            result.setCode(500);
            result.setMsg("根据id查询科室信息时发生异常");
        } else {
            result.setCode(200);
            result.setMsg("根据id查询科室信息成功");
            result.setData(department);
        }
        return result;
    }

    /**
     * 处理分页查询科室人员信息请求的Controller方法
     * @param pageNum 查询的分页页码数
     * @param pageSize 每一分页容纳的数据条数
     * @return 响应内容，通过分页查询得到的分页数据结果PageInfo封装的Result对象
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        List<Department> departmentList = adminDepartmentService.getByPage(pageNum, pageSize);

        Result result = new Result();
        if (departmentList == null) {
            result.setCode(500);
            result.setMsg("科室信息分页查询时发生异常，查询结果null");
        } else {
            PageInfo<Department> pageInfo = new PageInfo<>(departmentList);
            result.setCode(200);
            result.setMsg("科室信息分页查询成功");
            result.setData(pageInfo);
        }
        return result;
    }
}
